class Node{//节点
    constructor(element){
        this.element = element;
        this.prev = null;
        this.next = null;
    }
}
class DoubleList{
    constructor(){
        this.head = null;//首个节点
        this.tail = null;//尾部节点
        this.count = 0;//节点数量
    }
    push(element){//向链表尾部添加⼀个新元素。
        let node = new Node(element);
        if(this.head === null){//为空时
            this.head = node;
            this.tail = node;
        }else{
            let last = this.tail;
            this.tail = node;
            last.next = node;
            node.prev = last;
        }
        this.count++;
    }
    getElementAt(index){//查找指定元素
        let current = this.head;
        for(let i = 0;i < index;i++){
            current = current.next;
        }
        return current
    }
    insert(element,index){//向链表的特定位置插⼊⼀个新元素
        if(index >= 0 && index <= this.count){
            let node = new Node(element);
            if(index === 0){
                if(this.head === null){//为空时
                    this.head = node;
                    this.tail = node;
                }else{//不为空时
                    let current = this.head;
                    node.next = current;
                    current.prev = node;
                    this.head = node;
                }
            }else if(index === this.count){
                let last = this.tail;
                last.next = node;
                node.prev = last;
            }else{
                let prev = this.getElementAt(index - 1);
                let current = prev.next;
                prev.next = node;
                node.prev = prev;
                current.prev = node;
                node.next = current;
            }
            this.count++;
        }else{
            return false
        }
    }
    removeAt(index){//删除指定下标的元素
        if(index >= 0 && index < this.count){
            let current = this.head;
            if(index === 0){//开头删
                if(this.count === 1){//只有一个元素
                    this.head = null;
                    this.tail = null;
                }else{//多个
                    this.head = current.next;
                    current.next.prev = null;
                }
            }else if(index === (this.count-1)){//尾部删
                current = this.tail;
                this.tail = current.prev;
                this.tail.next = null;
            }else{//中间删
                current = this.getElementAt(index);
                current.prev.next = current.next;
                current.next.prev = current.prev;
            }
            this.count--;
            return current.element
        }else{
            return undefined
        }
    }
    indexOf(element){//返回元素在链表中的下标
        let current = this.head;
        for(let i = 0;i<this.count;i++){
            if(element === current.element){
                return i
            }
            current = current.next;
        }
        return -1
    }
    remove(element){
        let index = this.indexOf(element);//获取下标
        this.removeAt(index);
    }   
    
    size(){//元素节点的个数
        return this.count;
    }
    isEmpty(){//链表是否为空
        return this.count === 0
    }
    clear(){//清空链表中的元素
        this.head = null;
        this.tail = null;
        this.count = 0;
    }
    print(){//打印
        let current = this.head;
        for(let i = 0;i<this.count;i++){
            console.log(current);
            current = current.next;
        }
    }
}
let l = new DoubleList()
l.push("a")
l.getElementAt("a")
l.insert("b")
l.removeAt("b");
l.print();